package ace.module.sms.core.impl.controller.rest.admin.ext;

import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsUserAuthenticated;
import ace.module.sms.core.impl.model.request.SmsBatchSendRequest;
import ace.module.sms.core.impl.model.response.SmsBatchSendResponse;
import ace.module.sms.core.impl.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * template author: caspar
 * template created at: 2023-12-18
 * template description: admin controller
 * generated at: 2024年1月8日 下午3:53:01
 * description: 短信 admin controller
 */
@Tag(name = "短信-管理-API")
@PreAuthorizeIsUserAuthenticated
@AllArgsConstructor
@RestController
@RequestMapping(SmsAdminController.PATH)
@Valid
public class SmsAdminController {

  public static final String PATH = "/sms/admin";
  public static final String PATH_BATCH_SEND = "/batch/send";
  public static final String URL_BATCH_ADD = PATH + PATH_BATCH_SEND;
  private final SmsService smsService;

  @Operation(summary = "批量发送短信")
  @PostMapping(PATH_BATCH_SEND)
  public R<SmsBatchSendResponse> addBatch(@RequestBody SmsBatchSendRequest request) {
    SmsBatchSendResponse response = this.smsService.send(request);
    R.RBuilder<SmsBatchSendResponse> builder = R.builder();
    Integer code = response.getFailureCount() > 0 ? 1 : R.OK;
    builder.data(response);
    builder.code(code);
    builder.message(code.equals(R.OK) ? "成功" : "失败");
    return builder.build();
  }
}
